package com.ruoyi.customerSample.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import com.ruoyi.customerSample.domain.BackSampleBill;
import com.ruoyi.customerSample.domain.BackSampleDetail;
import com.ruoyi.customerSample.mapper.BackSampleBillMapper;
import com.ruoyi.customerSample.mapper.BackSampleDetailMapper;
import com.ruoyi.customerSample.service.IBackSampleBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 退样登记Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-15
 */
@Service
public class BackSampleBillServiceImpl implements IBackSampleBillService
{
    @Autowired(required = false)
    private BackSampleBillMapper backSampleBillMapper;

    @Autowired(required = false)
    private BackSampleDetailMapper backSampleDetailMapper;

    /**
     * 查询退样登记
     * 
     * @param id 退样登记ID
     * @return 退样登记
     */
    @Override
    public BackSampleBill selectBackSampleBillById(String id)
    {
        BackSampleBill bill = backSampleBillMapper.selectBackSampleBillById(id);
        bill.setList(backSampleDetailMapper.selectBackSampleDetailByBackId(id));
        return bill;
    }

    /**
     * 查询退样登记列表
     * 
     * @param backSampleBill 退样登记
     * @return 退样登记
     */
    @Override
    public List<BackSampleBill> selectBackSampleBillList(BackSampleBill backSampleBill)
    {
        List<BackSampleBill> list = backSampleBillMapper.selectBackSampleBillList(backSampleBill);
        if(list.size() > 0) {
            for(BackSampleBill backSample : list) {
                backSample.setList(backSampleDetailMapper.selectBackSampleDetailByBackId(backSample.getId()));
            }
        }
        return list;
    }

    /**
     * 新增退样登记
     * 
     * @param backSampleBill 退样登记
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBackSampleBill(BackSampleBill backSampleBill)
    {
        String id = IdUtils.fastSimpleUUID();
        backSampleBill.setId(id);
        backSampleBill.setRegistrant(SecurityUtils.getLoginUser().getUser().getNickName());
        int ret = backSampleBillMapper.insertBackSampleBill(backSampleBill);
        if(ret > 0) {
            //明细新增
            if(backSampleBill.getList().size() > 0) {
                for(BackSampleDetail backSampleDetail : backSampleBill.getList()) {
                    backSampleDetail.setBackId(id);
                    backSampleDetail.setId(IdUtils.fastSimpleUUID());
                }
                backSampleDetailMapper.batchBackSampleDetailBill(backSampleBill.getList());
            }
        }
        return ret;
    }

    /**
     * 修改退样登记
     * 
     * @param backSampleBill 退样登记
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBackSampleBill(BackSampleBill backSampleBill)
    {
        //删除原本明细并添加新明细
        if(backSampleBill.getList().size() > 0) {
            int ret = backSampleDetailMapper.deleteBackSampleDetailByBackId(backSampleBill.getId());
            if(ret > 0) {
                for(BackSampleDetail backSampleDetail : backSampleBill.getList()) {
                    backSampleDetail.setBackId(backSampleBill.getId());
                    backSampleDetail.setId(IdUtils.fastSimpleUUID());
                }
                backSampleDetailMapper.batchBackSampleDetailBill(backSampleBill.getList());
            }
        }
        return backSampleBillMapper.updateBackSampleBill(backSampleBill);
    }

    /**
     * 批量删除退样登记
     * 
     * @param ids 需要删除的退样登记ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBackSampleBillByIds(String[] ids)
    {
        //删除明细
        for(int i = 0; i < ids.length; i++) {
            backSampleDetailMapper.deleteBackSampleDetailByBackId(ids[i]);
        }
        return backSampleBillMapper.deleteBackSampleBillByIds(ids);
    }

    /**
     * 删除退样登记信息
     * 
     * @param id 退样登记ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBackSampleBillById(String id)
    {
        backSampleDetailMapper.deleteBackSampleDetailByBackId(id);
        return backSampleBillMapper.deleteBackSampleBillById(id);
    }
}
